<?php

  //
  // Memorii Web
  // script/db_user.php
  //
  // Access to the database
  //
  
  // Name of the table
  define('MII_TABLE_USERS', 'mii_users');
	
	function db_add_user($db_connection, $email, $password_md5, $root_folder_id) {
		db_clear_input($email);
		db_clear_input($password_md5);
		
		$query = 'INSERT INTO `'.MII_TABLE_USERS.'` (`email`, `password_md5`, `root_folder_id`, `validated`) VALUES (\''.$email.'\', \''.$password_md5.'\', \''.$root_folder_id.'\', 0)';
		db_query($db_connection, $query);
	}
	
	function db_get_root_folder_id($db_connection, $email, $password_md5) {
		db_clear_input($email);
		db_clear_input($password_md5);
		
		$query = 'SELECT `root_folder_id` FROM `'.MII_TABLE_USERS.'` WHERE (`email` = \''.$email.'\' AND `password_md5` = \''.$password_md5.'\' AND `validated`=\'1\') LIMIT 1';
		$result = db_query_array($db_connection, $query);
		if($result)
			return $result[0];
			
		return '0';
	}
	
	function db_activate_user($db_connection, $root_folder_id) {
		db_clear_input($root_folder_id);
		
		$query = 'UPDATE `'.MII_TABLE_USERS.'` SET `validated`=\'1\' WHERE `root_folder_id`=\''.$root_folder_id.'\' LIMIT 1';
		db_query($db_connection, $query);
	}
	
	function db_test_login($db_connection, $email, $password_md5) {
		db_clear_input($email);
		db_clear_input($password_md5);
		
		$query = 'SELECT * FROM `'.MII_TABLE_USERS.'` WHERE (`email` = \''.$email.'\' AND `password_md5` = \''.$password_md5.'\' AND `validated`=\'1\') LIMIT 1';
		$result = db_query_array($db_connection, $query);
		if($result)
			return true;
			
		return false;
	}
	
	function db_test_login_waiting_validation($db_connection, $email, $password_md5) {
		db_clear_input($email);
		db_clear_input($password_md5);
		
		$query = 'SELECT * FROM `'.MII_TABLE_USERS.'` WHERE (`email` = \''.$email.'\' AND `password_md5` = \''.$password_md5.'\') LIMIT 1';
		$result = db_query_array($db_connection, $query);
		
		if($result && $result['validated']=="0")
			return true;
			
		return false;
	}
?>